package cn.com.connext.scrm.service;

import cn.com.connext.scrm.domain.master.*;
import cn.com.connext.scrm.repository.master.*;
import cn.com.connext.scrm.service.dto.JsonResult;
import cn.com.connext.scrm.service.mapper.WhiskeyGiftsService;
import cn.com.connext.scrm.utils.EntityUtils;
import cn.com.connext.scrm.web.rest.req.AwardRecordUpdateReq;
import cn.com.connext.scrm.web.rest.req.DeleteReq;
import cn.com.connext.scrm.web.rest.req.WhiskeyGiftsCreateReq;
import cn.com.connext.scrm.web.rest.resp.*;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class WhiskeyGiftsServiceImpl implements WhiskeyGiftsService {
    private final Logger log = LoggerFactory.getLogger(WhiskeyGiftsServiceImpl.class);

    @Autowired
    private ActivityRepository activityRepository;

    @Autowired
    private NoteRepository noteRepository;

    @Autowired
    private WhiskeyGiftsRepository whiskeyGiftsRepository;

    @Autowired
    private AwardRecordRepository awardRecordRepository;

    @Autowired
    private MemberRepository memberRepository;

    @Override
    public ResponseEntity<List<WhiskeyGiftsResp>> queryNowGifts() {
        //查询当前启用的活动
        List<Activity> activityList = activityRepository.getCurrentActivityForAnswer();
        if (CollectionUtils.isEmpty(activityList)) {
            return ResponseEntity.ok(Collections.emptyList());
        }
        String id = activityList.get(0).getId();
        List<WhiskeyGifts> whiskeyGifts = whiskeyGiftsRepository.findByActivityId(id);
        if (CollectionUtils.isEmpty(whiskeyGifts)) {
            return ResponseEntity.ok(Collections.emptyList());
        }
        List<WhiskeyGiftsResp> whiskeyGiftsResps = EntityUtils.dtoList2PojoList(whiskeyGifts, WhiskeyGiftsResp.class);
        return ResponseEntity.ok(whiskeyGiftsResps);
    }

    @Override
    public ResponseEntity updateAwardRecord(AwardRecordUpdateReq req) {
        //根据id 查询帖子
        Note note = noteRepository.findOne(req.getNotId());
        if (null == note) {
            return ResponseEntity.ok("帖子不存在");
        }
        //查询当前启用的活动
        Activity activitie = activityRepository.getPostActivityForAnswer();
        if (activitie==null) {
            return ResponseEntity.ok("无已结束活动活动");
        }
        if (activitie.getId().compareTo(req.getActId()) != 0) {
            return ResponseEntity.ok("非当前活动无法设置中奖人");
        }
        //查询当前奖品
        List<WhiskeyGifts> whiskeyGiftsList = whiskeyGiftsRepository.findByActivityId(activitie.getId());
        if (CollectionUtils.isEmpty(whiskeyGiftsList)) {
            return ResponseEntity.ok("无奖品配置");
        }
        List<AwardRecord> awardRecords = awardRecordRepository.findByActivityId(activitie.getId());
        if (CollectionUtils.isNotEmpty(awardRecords)) {
            return ResponseEntity.ok("已设置中奖人，不可重复设置");
        }
        WhiskeyGifts whiskeyGifts = whiskeyGiftsList.get(0);
        AwardRecord awardRecord = new AwardRecord();
        awardRecord.setActivityId(activitie.getId());
        awardRecord.setAwardType("gift");
        awardRecord.setGiftId(whiskeyGifts.getId());
        awardRecord.setUnionId(note.getUserId());
        awardRecord.setAwardContent("威士忌之星活动一等奖");
        awardRecord.setCreateTime(new Date());
        awardRecord.setNoteId(req.getNotId());
        awardRecordRepository.saveAndFlush(awardRecord);
        return ResponseEntity.ok("设置中奖人成功");
    }

    @Override
    public ResponseEntity queryAwardRecord(String id) {
        AwardRecordResp awardRecordResp = new AwardRecordResp();
        JsonResult result = new JsonResult();
        List<AwardRecord> awardRecords = awardRecordRepository.findByActivityId(id);
        if (CollectionUtils.isEmpty(awardRecords)) {
            result.setState(1);
            result.setMessage("该活动不存在，请重新再试");
            return ResponseEntity.ok(result);
        }
        AwardRecord awardRecord = awardRecords.get(0);
        BeanUtils.copyProperties(awardRecord, awardRecordResp);
        Member member = memberRepository.findOneByUnionId(awardRecord.getUnionId());
        if (null != member) {
            awardRecordResp.setUserName(member.getAccount());
        }
        if (StringUtils.isNotBlank(awardRecord.getGiftId())) {
            WhiskeyGifts whiskeyGift = whiskeyGiftsRepository.findOne(awardRecord.getGiftId());
            if (StringUtils.isNotBlank(whiskeyGift.getName())) {
                awardRecordResp.setGiftName(whiskeyGift.getName());
            }
        }
        List<AwardRecordResp> awardRecordResps = new ArrayList<>();
        awardRecordResps.add(awardRecordResp);
        result.setData(awardRecordResps);
        result.setMessage("操作成功");
        result.setState(0);
        return ResponseEntity.ok(result);
    }

    @Override
    public ResponseEntity<WhiskeyAwardsResp> queryNowAward() {
        WhiskeyAwardsResp whiskeyAwardsResp = new WhiskeyAwardsResp();
        //查询当前启用的活动
        List<Activity> activities = activityRepository.getCurrentActivityForAnswer();
        if (CollectionUtils.isEmpty(activities)) {
            return ResponseEntity.ok(whiskeyAwardsResp);
        }
        List<AwardRecord> awardRecords = awardRecordRepository.findByActivityId(activities.get(0).getId());
        if (CollectionUtils.isEmpty(awardRecords)) {
            return ResponseEntity.ok(whiskeyAwardsResp);
        }
        AwardRecord awardRecord = awardRecords.get(0);
        whiskeyAwardsResp.setAwardContent(awardRecord.getAwardContent());
        whiskeyAwardsResp.setUnionId(awardRecord.getUnionId());
        return ResponseEntity.ok(whiskeyAwardsResp);
    }

    @Override
    public ResponseEntity homePage() {
        //当前活动
        List<WhiskeyHomePageResp> whiskeyHomePageResps = new ArrayList<>();
        List<Activity> activityList = activityRepository.getCurrentActivityForAnswer();
        if (CollectionUtils.isNotEmpty(activityList)) {
            WhiskeyHomePageResp now = cover(activityList.get(0).getId());
            now.setIsNow(1);
            now.setId(activityList.get(0).getId());
            whiskeyHomePageResps.add(now);
        }
        //往期活动
        Activity activity = activityRepository.getPostActivityForAnswer();
        if (null != activity) {
            WhiskeyHomePageResp post = cover(activity.getId());
            post.setIsNow(0);
            post.setId(activity.getId());
            whiskeyHomePageResps.add(post);
        }
        return ResponseEntity.ok(whiskeyHomePageResps);
    }

    @Transactional
    @Override
    public ResponseEntity<Boolean> create(WhiskeyGiftsCreateReq req) {
        WhiskeyGifts whiskeyGifts = new WhiskeyGifts();
        if (StringUtils.isBlank(req.getId())) {
            //保存
            BeanUtils.copyProperties(req, whiskeyGifts);
            whiskeyGifts.setCreateTime(new Date());
            whiskeyGifts.setUpdateTime(new Date());
            whiskeyGiftsRepository.saveAndFlush(whiskeyGifts);
            activityRepository.updateByActivityId(req.getActivityId());
            return ResponseEntity.ok(true);
        }
        //更新
        whiskeyGifts = whiskeyGiftsRepository.findOne(req.getId());
        if (null == whiskeyGifts) {
            return ResponseEntity.ok(false);
        }
        BeanUtils.copyProperties(req, whiskeyGifts);
        whiskeyGifts.setUpdateTime(new Date());
        whiskeyGiftsRepository.saveAndFlush(whiskeyGifts);
        return ResponseEntity.ok(true);
    }

    @Override
    public ResponseEntity<WhiskeyGiftsResp> detail(String id) {
        WhiskeyGiftsResp whiskeyGiftsResp = new WhiskeyGiftsResp();
        WhiskeyGifts whiskeyGifts = whiskeyGiftsRepository.findOne(id);
        if (null == whiskeyGifts) {
            return ResponseEntity.ok(whiskeyGiftsResp);
        }
        BeanUtils.copyProperties(whiskeyGifts, whiskeyGiftsResp);
        return ResponseEntity.ok(whiskeyGiftsResp);
    }

    private WhiskeyHomePageResp cover(String id) {
        WhiskeyHomePageResp whiskeyHomePageResp = new WhiskeyHomePageResp();
        //礼物
        List<WhiskeyGifts> whiskeyGifts = whiskeyGiftsRepository.findByActivityId(id);
        if (CollectionUtils.isNotEmpty(whiskeyGifts)) {
            whiskeyHomePageResp.setWhiskeyGifts(whiskeyGifts.get(0));
        }
        //中奖人
        List<AwardRecord> awardRecords = awardRecordRepository.findByActivityId(id);
        if (CollectionUtils.isNotEmpty(awardRecords)) {
            WhiskeyAwardsResp whiskeyAwardsResp = new WhiskeyAwardsResp();
            AwardRecord awardRecord = awardRecords.get(0);
            whiskeyAwardsResp.setAwardContent(awardRecord.getAwardContent());
            whiskeyAwardsResp.setUnionId(awardRecord.getUnionId());
            Member member = memberRepository.findOneByUnionId(awardRecord.getUnionId());
            if (null != member) {
                whiskeyAwardsResp.setAccount(member.getAccount());
            }
            whiskeyHomePageResp.setWhiskeyAwardsResp(whiskeyAwardsResp);
        }
        return whiskeyHomePageResp;
    }

}
